var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/index.ts
var src_exports = {};
__export(src_exports, {
  getQiankunConfig: () => getQiankunConfig,
  patchSlaveMenus: () => patchSlaveMenus
});
module.exports = __toCommonJS(src_exports);
var getQiankunConfig = (apps) => {
  const DeployEnv = !process.env.DEPLOY_ENV || process.env.DEPLOY_ENV === "/local" ? "/dev" : `/${process.env.DEPLOY_ENV}`;
  const microApps = [...apps ?? []];
  console.log("[drs] 本地注册的微应用包含：", microApps);
  return Promise.resolve({
    /** 注册的子应用 */
    apps: microApps.map((microApp) => ({
      name: microApp.name,
      entry: microApp.entry
    })),
    /** 子应用路径 */
    routes: microApps.map((microApp) => ({
      path: `/${microApp.name}`,
      microApp: microApp.name,
      microAppProps: {
        /** 自动设置微应用的加载状态 */
        autoSetLoading: true,
        /** 自动设置微应用的错误捕获 */
        autoCaptureError: true
      }
    })),
    /** 在第一个微应用 mount 完成后开始预加载其他微应用的静态资源 */
    prefetch: true,
    /** 若采用 strictStyleIsolation 样式严格隔离，shadow-dom 机制会导致 createPortal 创建的节点缺失样式 */
    sandbox: {},
    lifeCycles: {
      beforeLoad(app) {
        console.log("[drs] " + app.name + " 子应用加载前", app);
      },
      beforeMount(app) {
        console.log("[drs] " + app.name + " 子应用挂载前", app);
      },
      afterMount(app) {
        console.log("[drs] " + app.name + " 子应用挂载后", app);
      },
      beforeUnmount(app) {
        console.log("[drs] " + app.name + " 子应用卸载前", app);
      },
      afterUnmount(app) {
        console.log("[drs] " + app.name + " 子应用卸载后", app);
      }
    },
    /**
     * getPublicPath 影响子应用除 umi.js、umi.css 之外的资源加载路径
     * chunk 资源直接根据 html 访问地址查找
     * @param entry 子应用 html 访问地址
     * @returns
     */
    getPublicPath: (entry) => {
      const lastIndex = entry.indexOf("index.html");
      const microApp = microApps.find((app) => app.entry === entry);
      const publicPath = entry.slice(0, lastIndex);
      console.log("[drs] " + (microApp == null ? void 0 : microApp.name) + " 子应用 chunk 资源加载路径：", publicPath);
      return (publicPath == null ? void 0 : publicPath.endsWith("/")) ? publicPath : publicPath + "/";
    },
    /**
     * getTemplate 影响 umi.js、umi.css 等入口文件的加载路径
     * 支持子应用不设置 publicPath
     * @param 子应用 html 字符串内容
     * @returns
     */
    getTemplate: (tpl) => {
      var _a;
      const RouteBaseRegExp = /window.routerBase\s*=\s*\"[\/]*([a-z-]*)\"/;
      const microAppName = (_a = tpl.match(RouteBaseRegExp)) == null ? void 0 : _a[1];
      console.log("[drs] 从子应用 html 解析出的应用标识为：" + microAppName);
      const entryRegExp = /[^\"]*\/umi(\.[a-zA-Z0-9]*)?\.((js)|(css))/g;
      const microApp = microApps.find((app) => app.name === microAppName);
      const { useRuntimePublic } = microApp ?? {};
      if (!microAppName || !microApp || !useRuntimePublic) {
        console.log(
          "[drs] " + (microApp == null ? void 0 : microApp.name) + " 子应用 entry 资源加载路径将使用子应用 html 内容"
        );
        return tpl;
      }
      let baseUrl = microApp.entry.indexOf("/index.html") !== -1 ? microApp.entry.slice(0, microApp.entry.indexOf("/index.html")) : microApp.entry;
      baseUrl = baseUrl.indexOf("http") === 0 ? baseUrl : location.protocol + baseUrl;
      const result = tpl.replace(entryRegExp, ($0) => {
        console.log(
          "[drs] " + (microApp == null ? void 0 : microApp.name) + " 子应用 entry 资源加载路径：",
          baseUrl + $0
        );
        return baseUrl + $0;
      });
      console.log("[drs] " + (microApp == null ? void 0 : microApp.name) + " 子应用 html 实际使用内容" + result);
      return result;
    }
  });
};
var patchSlaveMenus = ({
  menus,
  apps
}) => {
  (apps ?? []).forEach((microApp) => {
    if (microApp.menu && !menus.some((menu) => menu.key === microApp.name)) {
      menus.push({
        key: microApp.name,
        path: microApp.to ? microApp.to : `/${microApp.name}`,
        menu: microApp.menu,
        title: microApp.title
      });
    }
  });
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getQiankunConfig,
  patchSlaveMenus
});
